import { getToken, setToken, removeToken } from '@/utils/auth'
import $httpAxios from '@/utils/httpAxios'
import router, { resetRouter } from '@/router'

const state = {
  token: getToken(),
  name: '',
  avatar: '',
  introduction: '',
  roles: [],
  userBaseInfo: {} // login返回来的数据
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_INTRODUCTION: (state, introduction) => {
    state.introduction = introduction
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  M_userBaseInfo: (state, userBaseInfo) => {
    state.userBaseInfo = userBaseInfo
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      // 发送登录请求
      // $httpAxios({
      //   url: 'rxdrepairsaas-usercenter/login', data: userInfo,
      //   method: 'post',isParams:true
      // }).then(res => {
      //   //需要将用户信息存储到本地
      //   setToken(res.access_token)
      //   localStorage.setItem("L_userBaseInfo",JSON.stringify(res.data))
      //   resolve()
      // }).catch(error => {
      //   reject(error)
      // })
      /* dd-此处无登录进入 如需登录请使用上面逻辑*/
      // 模拟登录请求回来的数据
      const userInfo = {
        unitId: 1,
        username: 'kuanghua',
        companyName: '后台pc基础架构',
        roleName: '管理员'
      }
      localStorage.setItem("L_userBaseInfo",JSON.stringify(userInfo))
      setToken('test_token')
      //commit('M_userBaseInfo', userInfo)
      resolve()
    })
  },
  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      // roles must be a non-empty array
      const roles = ['admin']
      if (!roles || roles.length <= 0) {
        reject('getInfo: roles must be a non-null array!')
      }
      commit('SET_ROLES', roles)
      // commit('SET_NAME', state.userBaseInfo.username)
      // commit('SET_AVATAR', state.userBaseInfo.logoRoute)
      // commit('SET_INTRODUCTION', state.userBaseInfo.companyName)
      resolve(roles)
    })
  },
  // user logout
  logout({ commit, state, dispatch }) {
    return new Promise((resolve, reject) => {
      $httpAxios({
        url: 'rxdrepairsaas-usercenter/logout',
        method: 'get'
      }).then(() => {
        commit('SET_TOKEN', '')
        commit('SET_ROLES', [])
        removeToken()
        resetRouter()
        // reset visited views and cached views
        // to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2485
        dispatch('tagsView/delAllViews', null, { root: true })
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      removeToken()
      resolve()
    })
  },

  // dynamically modify permissions
  async changeRoles({ commit, dispatch }, role) {
    const token = role + '-token'
    commit('SET_TOKEN', token)
    setToken(token)

    const { roles } = await dispatch('getInfo')

    resetRouter()

    // generate accessible routes map based on roles
    const accessRoutes = await dispatch('permission/generateRoutes', roles, { root: true })
    console.log('accessRoutes', accessRoutes)
    // dynamically add accessible routes
    console.log('动态添加路由')
    router.addRoutes(accessRoutes)

    // reset visited views and cached views
    dispatch('tagsView/delAllViews', null, { root: true })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
